<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Orders extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['order_ip','order_gid' => 1,'order_ispay'=> 1,'order_addtime'];
	protected $update = ['order_paytime','order_confirmtime'];
	public $fieldlist = [
		'type_list' => ['alipay'=>'支付宝','weixin'=>'微信','paypal'=>'PayPal','atfpay'=>'云支付','codepay1'=>'码支付支付宝','codepay2'=>'码支付QQ钱包','codepay3'=>'码支付微信'],
		'status_list' =>[1=>'未确认',2=>'已确认',3=>'已取消',4=>'无效',5=>'退货'],
		'ispay_list' => [1=>'未付款',2=>'付款中',3=>'已付款']
	];
    public function getOrderTypeTextAttr($value,$data){
		$fieldlist=$this->fieldlist['type_list'];
        return $fieldlist[$data['order_type']];
    }
    public function getOrderStatusTextAttr($value,$data){
		$fieldlist=$this->fieldlist['status_list'];
        return $fieldlist[$data['order_status']];
    }
    public function getOrderIspayTextAttr($value,$data){
		$fieldlist=$this->fieldlist['ispay_list'];
        return $fieldlist[$data['order_ispay']];
    }	
	protected function setOrderIpAttr(){
        return bindec(decbin(ip2long(request()->ip())));
    }
	protected function setOrderAddtimeAttr(){
        return time();
    }	
	protected function setOrderPaytimeAttr(){
        return time();
    }
	protected function setOrderConfirmtimeAttr(){
        return time();
    }
    public function user(){
        return $this->hasOne('user','user_id','order_uid');
    }		
	//根据订单号修改订单状态及更新用户影币
	public function update_order($sign,$money,$type=2){
		$where = array();
		$where['order_sign'] = array("eq", $sign);
		//查询订单
		$info = $this->field('order_uid,order_ispay,order_money')->where($where)->find();
		$user = model('user')->where('user_id',$info['order_uid'])->find();
		if(empty($user['user_id'])){
			$this->error = '用户不存在';
			return false;
		}
		//未付款状态用金额相同
		if(($info['order_ispay'] < 3) && ($info['order_money']==$money)){
			//更新订单状态
			if($this->save(['order_status'=>2,'order_ispay'=>3,'order_shipping'=>1],$where)!== false){
				$score=model('Score');
			    //更新用户积分
				if($score->user_score($info['order_uid'],$type,intval($info['order_money']*$GLOBALS['config']['user']['pay']['pay_scale']),0,0)!== false){
					return true;
				}else{
			        $this->error = $this->getError();
			        return false;					
				}
			}else{
			    $this->error = '更新订单失败';
			    return false;				
			}
		}
	}
	public function getlist($where,$page){
		$list = $this->where($where)->order('order_addtime desc ')->paginate($page['limit'],false,['page'=>$page['currentpage']]); 
		return $list ;
	}
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='order_'.$param['order'].' desc';
		}		
		if(empty($tag['order'])){
			$tag['order']='order_addtime desc';
		}	
		$where=[];
	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['order_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['order_uid'] = array('eq',$uids[0]);
		    }
	    }					
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['order_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['order_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
			$route=$GLOBALS['route'];
            $url=urldecode(url($route,config('params')));
		    $data = $this->join('user u','u.user_id=order_uid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = $this->join('user u','u.user_id=order_uid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}	
		return $data;
    }	
	
}